<template>
  <div class="my-container">
    <!-- 用户信息 -->
    <div class="shangfanganniu"><van-icon name="revoke" size="25" color="#1989fa" @click="$router.go(-1)" /></div>
    <van-loading v-if="!autor" type="spinner" color="#1989fa" />
    <div v-else class="header user-info">
      <div class="base-info">
        <div class="left">
          <img class="avatar" :src="imgP" @click="showImg">
          <span class="name">{{ userInfo.name }}</span>
          <div class="guanzhu"><van-button v-if="!guanzhu" icon="plus" type="primary" @click="addLikeAutor"/><van-button class="yiguanzhu" v-else icon="success" type="primary" @click="addLikeAutor" /><span>{{isGuanzhu}}</span></div>
        </div>
      </div>
      <div class="data-stats">
        <div class="data-item">
          <span class="count">{{ userInfo.fans_count }}</span>
          <span class="text" @click="$router.push(`/fans/${userInfo.name}/0`)">粉丝</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userInfo.follow_count }}</span>
          <span class="text" @click="$router.push(`/fans/${userInfo.name}/1`)">我的关注</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userInfo.art_count }}</span>
          <span class="text" @click="$router.push('/404')">相关评论</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userInfo.like_count }}</span>
          <span class="text" @click="$router.push('/404')">获赞</span>
        </div>
      </div>
    </div>
    <!-- 宫格导航 -->
    <van-grid class="grid-nav mb-9" :column-num="2" clickable>
      <van-grid-item class="grid-item">
        <i slot="icon" class="toutiao toutiao-shoucang"></i>
        <span slot="text" class="text" @click="$router.push('/404')">收藏</span>
      </van-grid-item>
      <van-grid-item class="grid-item">
        <i slot="icon" class="toutiao toutiao-lishi"></i>
        <span slot="text" class="text" @click="$router.push('/404')">历史</span>
      </van-grid-item>
    </van-grid>
    <!-- /宫格导航 -->

    <van-cell title="联系作者" @click="$router.push(`/chathistory/${userInfo.name}`)" is-link />
    <van-cell class="mb-9" title="相关作品" @click="$router.push('/404')" is-link />
  </div>
</template>

<script>
import { ImagePreview } from 'vant'
import { mapState } from 'vuex'
export default {
  name: 'MyIndex',
  components: {
    [ImagePreview.Component.name]: ImagePreview.Component
  },
  data () {
    return {
      name: this.$route.params.name,
      id: this.$route.params.num,
      userInfo: {},
      imgP: '',
      autor: false
    }
  },
  computed: {
    ...mapState(['autorlist', 'autorInfo']),
    autorinfo () {
      return this.autorlist.find(val => val.name === this.name)
    },
    guanzhu () {
      if (this.autorInfo.length > 0) {
        return this.autorInfo.findIndex(val => val.name === this.name) !== -1
      } else {
        return false
      }
    },
    isGuanzhu () {
      return this.guanzhu ? '已关注' : '未关注'
    }
  },
  created () {
    this.autor = false
    setTimeout(() => {
      this.userInfo = this.autorinfo
      var p = this.autorinfo.photo
      this.imgP = require('@/assets/touxiang/' + p)
      this.autor = true
    }, 1000)
  },
  methods: {
    showImg () {
      ImagePreview({
        images: [
          this.imgP
        ],
        closeable: true
      })
    },
    addLikeAutor () {
      if (!this.guanzhu) {
        this.$toast.success('已关注')
        this.$store.commit('addLikeAutor', this.autorinfo)
      } else {
        this.$toast.fail('已取消关注')
        this.$store.commit('removeLikeAutor', this.autorinfo.name)
      }
    }
  }
}
</script>

<style scoped lang="less">
  .my-container {
    .header

  {
    height: 361px;
    background: url("~@/assets/backg.jpg");
    background-size: cover;
  }

  .not-login {
    display: flex;
    justify-content: center;
    align-items: center;
    .login-btn

  {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .mobile-img

  {
    width: 132px;
    height: 132px;
    margin-bottom: 15px;
  }

  .text {
    font-size: 28px;
    color: #fff;
  }

  }
  }

  .user-info {
    .base-info

  {
    height: 231px;
    padding: 76px 32px 23px;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .left

  {
    display: flex;
    align-items: center;
    .avatar

  {
    width: 132px;
    height: 132px;
    border: 4px solid #fff;
    margin-right: 23px;
    border-radius: 50%;
  }

  .name {
    font-size: 30px;
    color: #fff;
  }

  }
  }

  .data-stats {
    display: flex;
    .data-item

  {
    height: 130px;
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    color: #fff;
    .count

  {
    font-size: 36px;
  }

  .text {
    font-size: 23px;
  }

  }
  }
  }

  .grid-nav {
    .grid-item

  {
    height: 141px;
    i .toutiao

  {
    font-size: 45px;
  }

  .toutiao-shoucang {
    color: #eb5253;
  }

  .toutiao-lishi {
    color: #ff9d1d;
  }

  span.text {
    font-size: 28px;
  }

  }
  }

  .logout-cell {
    text-align: center;
    color: #d86262;
  }

  .mb-9 {
    margin-bottom: 9px;
  }
  }
  .shangfanganniu {
    position: fixed;
    top: 20px;
    width: 100px;
    height: 60px;
    right: 0;
    z-index: 999;
  }
  .van-loading{
    margin:150px 350px;
  }
  .guanzhu{
      position:absolute;
      right:40px;
      top:110px;
      .yiguanzhu{
                    background-color:#b7b4b4!important;
                }
  .van-button
  {
    width: 65px;
    height: 65px;
    background-color: #d75f95;
    margin:0 0 5px 2px;
    border: 0;
    &:ative
  {
    background-color: #eb5253;
  }
  }
  span {
    color: #fff;
    font-size: 20px;
    display: block;
  }

  }
</style>
